#include <iostream>
#include <fstream>


#include "TROOT.h"
#include "TApplication.h"
#include "TFile.h"
#include "TMath.h"
#include <math.h>

#include "HeaderParameters.hh"
#include "RawData.hh"
#include "Analysis.hh"

using std::cout;
using std::cin;
using std::endl;
using std::ofstream;
using std::string;



int main(int argc, char **argv) {

	string headerfilename;
	string datafilename;
	string rootfilename;
	string basefilename;
	 
	float voltage_threshold;
	float variance_threshold;
	int noise_histo;
	int count_peaks;
	int charge_histo;
	int variance_histo;
	int variance_cut;
	int cut_zeroes;
	int channels;
	
	if (argc<=2)
	{
		cout << "Usage: 'TraceToRoot [datafile] [data headerfile] [outputfile] [voltage threshold] [noise histo (0/1)] [count_peaks (0/1)] [charge_histo (0/1)] [variance_histo (0/1)] [variance_cut (0/1)] [variance_threshold] [cut zeroes (0/1)]" << endl;
	}

	else 
	{
		datafilename = argv[1];
		headerfilename = argv[2];
		rootfilename = argv[3];
	
		voltage_threshold = atof(argv[4]);
		noise_histo = atoi(argv[5]);
		count_peaks = atoi(argv[6]);
		charge_histo = atoi(argv[7]);
		variance_histo = atoi(argv[8]);
		variance_cut = atoi(argv[9]);
		variance_threshold = atof(argv[10]);
		cut_zeroes = atoi(argv[11]);
		channels=atoi(argv[12]);

		cout << endl;	
		cout << "Voltage threshold (used to count peaks): " << voltage_threshold << " Volts" << endl;
		cout << "Histogram voltage noise (1/0 = yes/no): " << noise_histo << endl;
		cout << "Count peaks (count empty traces) (1/0 = yes/no): " << count_peaks << endl;
		cout << "Histogram charge (number of electrons) (1/0 = yes/no): " << charge_histo << endl;
		cout << "Histogram variance (of 1st 10 percent of waveforms for variance cut) (1/0 = yes/no): " << variance_histo << endl;
		cout << "Perform variance cut to eliminate false negative charges (1/0 = yes/no): " << variance_cut << endl;
		cout << "Variance threshold (used by variance cut): " << variance_threshold << endl;
		cout << "Don't histogram zero traces in trace_electrons (1/0 = yes/no): " << cut_zeroes << endl;
		cout << "Number of channels" << channels << endl;
		cout << endl;	
	
		size_t found_period = datafilename.find_first_of(".");
		size_t found_under = datafilename.find_first_of("_");
		size_t found_dir = datafilename.find_first_of("/");
		size_t end,start = 0;
	
		if (found_dir<found_period || found_dir<found_under)
			start = found_dir;
		if (found_period<found_under)
			end = found_period;
		else
		end = found_under;
	
		basefilename = datafilename.substr(start+1, end-(start+1));
		cout << "BASEFILENAME: " << basefilename << endl << endl;
	
	}
	
	HeaderParameters* header = new HeaderParameters(headerfilename, channels);
	header->PrintParameters();
	cout << endl;
	
	//START ROOT
	TApplication appl("App", &argc, argv);
	
	RawData* data = new RawData(datafilename, header, channels);
	Analysis* analysis = new Analysis(rootfilename, header, data, voltage_threshold, variance_threshold, channels);
	TH1F** waveform;

	float time_per_div = 0; //DUMMY!!!
/*	
	float time_per_div = header->time_per_div;
	cout << "timescale: " << time_per_div << endl;
	
	const int num_traces = header->num_traces;
	cout << "numtraces: " << num_traces << endl;
*/	
	const int num_traces = header->GetParameter(0,7);
	bool is_empty;
	int i;

	bool in_var_threshold;

	//~ cout << endl;
	//~ cout << "Begin rangefinding loop" << endl;	
	//~ cout << endl;
//~ 
	//~ for (i=0; i<num_traces; i++)
	//~ {
		//~ waveform = data->LoadNextWaveforms(time_per_div);
//~ 
		//~ if (waveform==NULL)
		//~ {
		 	 //~ cout << "BROKE!!!" << endl;
		 	 //~ break;
		//~ } 
//~ 
//~ //		if (variance_histo == 1)
//~ //		{
//~ //			analysis->variance_minmax();
//~ //		}
		 //~ 
//~ //		if (variance_cut == 1)
//~ //		{
//~ //			in_var_threshold = analysis->variance_cut(true);
//~ //		}
//~ //		else
//~ //		{
//~ //			in_var_threshold = true;
//~ //		}
//~ 
//~ //		if (in_var_threshold == true)
//~ //		{
			//~ if(count_peaks == 1)
			//~ {
				//~ is_empty = analysis->count_peaks(true);
			//~ }
//~ 
			//~ if (noise_histo == 1)
			//~ {
				//~ analysis->noise_minmax();
			//~ }
			//~ 
			//~ if(cut_zeroes == 1)
			//~ {
				//~ if(is_empty == false)
				//~ {	
					//~ if (charge_histo == 1)
					//~ {
						//~ analysis->charge_minmax();
					//~ }
				//~ }
			//~ }
//~ 
			//~ if(cut_zeroes == 0)
			//~ {
				//~ if (charge_histo == 1)
				//~ {
					//~ analysis->charge_minmax();
				//~ }
			//~ }
//~ 
		//~ //}
//~ 
		//~ if (i%100==0)
		//~ cout << "Processed Trace: " << i << endl;
	//~ }	 
//~ 
//~ 
//~ 
//~ 
	//~ if (noise_histo == 1)
	//~ {
		//~ analysis->noise_range();
	//~ }
//~ 
//~ //	if (variance_histo == 1)
//~ //	{
//~ //		analysis->variance_range();
//~ //	}
	//~ 
	//~ if (charge_histo == 1)
	//~ {
		//~ analysis->charge_range();
	//~ }
//~ 
//~ cout << "DONE CHARGRANGE" << endl;
	delete data;
cout << "DONE DELDAT" << endl;
	data = new RawData(datafilename, header, channels);
cout << "DONE NEWDAT" << endl;
	
	cout << endl;
	cout << "Begin analysis loop" << endl;	
	cout << endl;


	for (i=0; i<num_traces; i++)
	{
		bool writewaveform = false;
		if (i<1)
		writewaveform = true;
	
		waveform = data->LoadNextWaveforms(time_per_div);
		
		if (waveform==NULL)
		{
		 	 cout << "BROKE!!!" << endl;
		 	 break;
		} 
	 
//		if(variance_histo == 1)
//		{
//			analysis->variance_histo(); 
//		}

//		if (variance_cut == 1)
//		{
//			in_var_threshold = analysis->variance_cut(false);
//		}
//		else
//		{
//			in_var_threshold = true;
//		}

//		if(in_var_threshold == true)
//		{
			//~ if(noise_histo == 1)
			//~ {
				//~ analysis->noise_histo(); 
			//~ }
		
			if(count_peaks == 1)
			{
				is_empty = analysis->count_peaks(false);
			}
		
		//	if (is_empty == false)
		//	if (in_var_threshold == false)
		//	{
		//		writewaveform = true;
		//	}	

			//~ if(cut_zeroes == 1)
			//~ {
				//~ if(is_empty == false)
				//~ {	
					//~ if (charge_histo == 1)
					//~ {
						//~ analysis->charge_histo(writewaveform);
					//~ }
				//~ }
			//~ }
//~ 
			//~ if(cut_zeroes == 0)
			//~ {
				//~ if (charge_histo == 1)
				//~ {
					//~ analysis->charge_histo(writewaveform);
				//~ }
			//~ }
		
		//}
		analysis->charge_histo(writewaveform);
		analysis->filltree();
		if (i%100==0)
		cout << "Processed Trace: " << i << endl;
		

	}

	cout << "OUTLOOP" << endl;

	cout << endl;
	
//	if(variance_cut == 1)
//	{
//		analysis->write_variance_cut();
//	}
	
	//~ if(noise_histo == 1)
	//~ {
		//~ analysis->write_noise_histo();
	//~ }
	
//	if(variance_histo == 1)
//	{
//		analysis->write_variance_histo();
//	}	
	
	if(count_peaks == 1)
	{
		analysis->write_count_peaks();
	}
	
	//~ if(charge_histo == 1)
	//~ {
		//~ analysis->write_charge_histo();
	//~ }
	//~ 
	delete analysis;
	delete data;
	delete header;
	cout << endl << "FIN" << endl;
	
	return 0;
	
	}
